import sys, locale

def main():
  locale.setlocale(locale.LC_ALL, "")
  if len(sys.argv) != 3:
    print "Usage: sortidx <InputIndex> <OutputIndex>"
    return

  f = file(sys.argv[1], "rb")
  idx = []
  for l in f:
    w, s, e = l.split('\t')
    idx += [ (w.decode("utf-8"), s, e) ]
  f.close()

  ucmp = cmp
  # ucmp = locale.strcoll # Do NOT use, Python S60 doesn't know locales

  idx.sort(lambda l,r: ucmp(l[0].lower(), r[0].lower()))
  
  f = file(sys.argv[2], "wb")
  for w, s, e in idx:
    f.write('\t'.join([w.encode("utf-8"), s, e]))
  f.close()

if __name__ == "__main__":
  main()
